package com.espirit.eap.sa;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.espirit.EntityMapping;
import com.googlecode.cswish.model.Saas;

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "userType", discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorValue("0")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class UserTemplate extends EntityMapping implements Serializable, Saas {
	
	private static final long serialVersionUID = -4606737144749469099L;
	
	private String name;
	
	private String description;
	
	// user <----> selected User privilege (page privilege, reference the privilege id) <----> field privilege <FieldPermission>
	private List<UserTemplatePrivilege> userTemplatePrivileges;
	
	// user <----> selected Model privilege <ModelFieldPrivilege>
	private List<ModelFieldPermission> modelFieldPermissions;
	
	// TODO: remove it?
	// Base template
	private UserTemplate userTemplate;
	
	// interface field, the selected product
	// TODO: Refactor it to the productName
	private Website defaultWebsite;
	
	private Integer sid;
	public Integer getSid() {
		return sid;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}
	
	@Column(nullable=false, unique=false, length=50)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * It's the page permission & page field permission
	 * 
	 * @return
	 */
	@OneToMany(targetEntity=UserTemplatePrivilege.class ,orphanRemoval = true,fetch=FetchType.LAZY,cascade=CascadeType.ALL,mappedBy="userTemplate")
	public List<UserTemplatePrivilege> getUserTemplatePrivileges() {
		return userTemplatePrivileges;
	}

	public void setUserTemplatePrivileges(
			List<UserTemplatePrivilege> userTemplatePrivileges) {
		this.userTemplatePrivileges = userTemplatePrivileges;
	}

	public List<UserTemplatePrivilege> safeUserTemplatePrivileges() {
		if (userTemplatePrivileges == null){
			userTemplatePrivileges = new ArrayList<UserTemplatePrivilege>();
		}
		return userTemplatePrivileges;
	}
	
	
	@ManyToOne(fetch=FetchType.LAZY)
	public UserTemplate getUserTemplate() {
		return userTemplate;
	}

	public void setUserTemplate(UserTemplate userTemplate) {
		this.userTemplate = userTemplate;
	}
	
	@Transient
	public Website getDefaultWebsite() {
		return defaultWebsite;
	}

	public void setDefaultWebsite(Website defaultWebsite) {
		this.defaultWebsite = defaultWebsite;
	}

	@OneToMany(targetEntity=ModelFieldPermission.class ,orphanRemoval = true,fetch=FetchType.LAZY,cascade=CascadeType.ALL,mappedBy="userTemplate")
	public List<ModelFieldPermission> getModelFieldPermissions() {
		return modelFieldPermissions;
	}

	public void setModelFieldPermissions(List<ModelFieldPermission> modelFieldPermissions) {
		this.modelFieldPermissions = modelFieldPermissions;
	}	
	
	public List<ModelFieldPermission> safeModelFieldPermissions() {
		if (modelFieldPermissions == null){
			modelFieldPermissions = new ArrayList<ModelFieldPermission>();
		}
		return modelFieldPermissions;
	}
	
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Transient
	public List<Privilege> getPagePermissions(){
		if (this.getUserTemplatePrivileges() == null ) return Collections.EMPTY_LIST;
		List<Privilege> results = new ArrayList<Privilege>();
		for(UserTemplatePrivilege userTemplatePrivilege : this.getUserTemplatePrivileges()){
			results.add(userTemplatePrivilege.getPrivilege());
		}
		return results;
	}
	
	@Transient
	public List<PageFieldPermission> getPageFieldPermissions(){
		if (this.getUserTemplatePrivileges() == null ) return Collections.EMPTY_LIST;
		List<PageFieldPermission> results = new ArrayList<PageFieldPermission>();
		for(UserTemplatePrivilege userTemplatePrivilege : this.getUserTemplatePrivileges()){
			if (userTemplatePrivilege.getPageFieldPermissions() != null){
				results.addAll(userTemplatePrivilege.getPageFieldPermissions());
			}
		}
		return results;
	}
	
}